import 'package:development_assistant/config/colors.dart';
import 'package:development_assistant/controller/server_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../../config/font.dart';
class AddServer extends StatelessWidget {

  final ServerController serverController=Get.put(ServerController());
  AddServer({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _appBar(),
      bottomNavigationBar: _bottomBtn(),
      resizeToAvoidBottomInset: false,
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 20.w,horizontal: 20.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text("服务器信息".tr,style: FontConfig.titleStyle,),
              _serverField("服务器类型".tr,serverController.serverType),
              _serverField("服务器名称".tr,serverController.serverName),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  _serverField("主机号".tr,serverController.serverHost),
                  _serverField("端口号".tr,serverController.serverPort),
                ],
              ),
              _serverField("Url",serverController.serverUrl),
              Text("测试连接".tr,style: FontConfig.titleStyle,),
              _testConnect(),
            ],
          ),
        ),
      ),
    );
  }

  _bottomBtn(){
    return Container(
        width: 350.w,
        height: 35.h,
        margin: EdgeInsets.symmetric(vertical: 10.h),
        child: Center(
          child: ElevatedButton(
            onPressed: (){
              Get.defaultDialog(
                title: "保存服务器信息".tr,
                middleText: "${'是否保存服务器信息?'.tr}: ${serverController.serverName}",
                confirm: TextButton(
                  style: TextButton.styleFrom(
                      backgroundColor: ColorsConfig().buttonColorOfTheme,
                      foregroundColor: Colors.white,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10)
                      )
                  ),
                  onPressed: (){
                    serverController.addServer();
                  },
                  child: Text("确定".tr),
                ),
                cancel:TextButton(
                  style: TextButton.styleFrom(
                      backgroundColor: ColorsConfig().buttonColorOfTheme,
                      foregroundColor: Colors.white,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10)
                      )
                  ),
                  onPressed: (){
                    Get.back();
                  },
                  child:Text("取消".tr),
                ),
              );
            },
            style: ElevatedButton.styleFrom(
                backgroundColor: ColorsConfig().buttonColorOfTheme,
                foregroundColor: Colors.white,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20)
                )
            ),
            child: Text("保存服务器信息".tr,style: TextStyle(fontWeight: FontWeight.bold),),
          ),
        )
    );
  }

  _serverField(String hintText,RxString serverValue){
    return Container(
      width: hintText=="主机号"||hintText=="端口号"||hintText=="Host"||hintText=="Port"?150.w:320.w,
      height: 50.h,
      margin: EdgeInsets.symmetric(vertical: 5.h),
      child: Center(
        child: TextField(
          maxLines: 1,
          decoration: InputDecoration(
              hintText:hintText,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15),
              )
          ),
          onChanged: (e){
            serverValue.value=e;
          },
        ),
      ),
    );
  }

  _testConnect(){
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            ElevatedButton.icon(
              onPressed: (){
                serverController.link();
              },
              icon: const Icon(Icons.link),
              label: Text("连接".tr),
              style: ElevatedButton.styleFrom(
                  backgroundColor: ColorsConfig().buttonColorOfTheme,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  )
              ),
            ),
            Obx(() => serverController.linkStatue.value!=0?Row(
              children: [
                Container(
                  width: 10,
                  height: 10,
                  margin: EdgeInsets.symmetric(horizontal: 10.w),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(50),
                      color: serverController.linkStatue.value==200?Colors.green:Colors.red
                  ),
                ),
                Text(serverController.linkStatue.value.toString(),style: FontConfig.fieldTitle,),
              ],
            ):Container())
          ],
        ),
        Container(
            height: 140.h,
            margin: EdgeInsets.symmetric(vertical: 10.h),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                border: Border.all(color:Colors.grey,width: 1)
            ),
            child: Obx(()=>TextField(
              controller: TextEditingController(text: serverController.linkResult.value),
              maxLines: null,
              decoration: const InputDecoration(
                  border: OutlineInputBorder(
                      borderSide: BorderSide.none
                  )
              ),
              onChanged: (e){

              },
            ))
        ),
      ],
    );
  }

  _appBar(){
    return AppBar(
      title: Text("添加服务器".tr),
      centerTitle: true,
      leading: IconButton(
        onPressed: (){
          Get.offNamed("/Home",arguments: {
            "selectIndex":0
          });
        },
        icon: const Icon(Icons.arrow_back),
      ),
    );
  }
}
